package pe.edu.claretiano.admisionmatricula.matricula.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import pe.edu.claretiano.admisionmatricula.bean.CronogramaEvaluacion;
import pe.edu.claretiano.admisionmatricula.bean.Matricula;
import pe.edu.claretiano.admisionmatricula.bean.SolicitudMatricula;
import pe.edu.claretiano.admisionmatricula.factory.MyBatisConnectionFactory;
import pe.edu.claretiano.admisionmatricula.matricula.dao.MatriculaDao;

public class MatriculaDaoImpl implements MatriculaDao {
	
	private final static Logger logdMatriculaDaoImpl = Logger.getLogger(MatriculaDaoImpl.class);

	private SqlSessionFactory sqlSessionFactory;

	public MatriculaDaoImpl() {
		sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
	}

	@Override
	public Integer registrarMatricula(Matricula matricula) {
		SqlSession session = sqlSessionFactory.openSession();
		Integer resultado = 0;
		try {
			resultado = session.insert("Matricula.insertarMatricula",matricula);
			session.commit();
		} catch (Exception ex) {
			logdMatriculaDaoImpl.error(ex.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return resultado;
	}
	
	@Override
	public List<Matricula> listarMatriculaCons(
			Matricula objMatriculaCons) {
		SqlSession session = sqlSessionFactory.openSession();
		List<Matricula> lstMatricula = new ArrayList<Matricula>();
		try {
			lstMatricula = (List<Matricula>) session.selectList(
					"Matricula.listarMatriculaCons", objMatriculaCons);
		} catch (Exception ex) {
			logdMatriculaDaoImpl.error(ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			session.close();
		}
		return lstMatricula;
	}


	@Override
	public SolicitudMatricula obtenerMatriculaRegistrada(SolicitudMatricula solicitudMatricula) {
		SqlSession session = sqlSessionFactory.openSession();
		SolicitudMatricula solicitudMat = null;
		try {
			solicitudMat = (SolicitudMatricula) session.selectOne("Matricula.obtenerMatriculaRegistrada",solicitudMatricula);
		} catch (Exception ex) {
			logdMatriculaDaoImpl.error(ex.getMessage());
		} finally {
			session.close();
		}
		
		return solicitudMat;
	}

	@Override
	public void actualizarNumeroMatricula(Matricula matricula) {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			session.update("Matricula.actualizarNumeroMatricula",matricula);
			session.commit();
		} catch (Exception ex) {
			logdMatriculaDaoImpl.error(ex.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		
	}

}